package com.sj.androidvoicedemo;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class VoiceDemoActivity extends Activity {

	public boolean isLongClick() {
		return isLongClick;
	}

	public void setLongClick(boolean isLongClick) {
		this.isLongClick = isLongClick;
	}

	public boolean isRecording() {
		return isRecording;
	}

	public void setRecording(boolean isRecording) {
		this.isRecording = isRecording;
	}

	private TextView showTxt = null;
	private boolean isRecording = false;
	private boolean isLongClick = false;
	private ProgressBar recordingProgressBar = null;
	private MediaRecorder mRecorder = null;
	private String PATH_NAME = "";
	private final String FILE_NAME = "tempsound.3gp";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_voice_demo);

		Button voiceBtn = (Button) findViewById(R.id.voiceBtn);
		showTxt = (TextView) findViewById(R.id.showTxt);
		recordingProgressBar = (ProgressBar) findViewById(R.id.recordingProgressBar);
		Button playBtn = (Button) findViewById(R.id.playBtn);

		voiceBtn.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				if (!isRecording()) {
					recording(true);
					setRecording(true);
					showTxt.setText("I'm touching!");
					setLongClick(true);
					recordingProgressBar.setVisibility(ProgressBar.VISIBLE);
				}
				return false;
			}
		});

		voiceBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isLongClick()) {
					Toast.makeText(VoiceDemoActivity.this, "请长按按钮！",
							Toast.LENGTH_SHORT).show();

				}
				setLongClick(false);
			}
		});

		voiceBtn.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					if (isRecording()) {
						recording(false);
						setRecording(false);
						showTxt.setText(showTxt.getHint());
						recordingProgressBar
								.setVisibility(ProgressBar.INVISIBLE);
					}
					break;

				default:
					break;
				}
				return false;
			}

		});

		playBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MediaPlayer mPlayer = new MediaPlayer();
				try {
					mPlayer.setDataSource(PATH_NAME);
					mPlayer.prepare();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				mPlayer.start();
			}
		});

	}

	public void recording(boolean flat) {
		try {
			if (!Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				PATH_NAME = Environment.getDownloadCacheDirectory()
						.getCanonicalPath() + File.separator + FILE_NAME;
			} else {
				PATH_NAME = Environment.getExternalStorageDirectory()
						.getCanonicalPath() + File.separator + FILE_NAME;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (flat) {
			Log.d("recording", flat + "");
			mRecorder = new MediaRecorder();
			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			try {
				mRecorder.setOutputFile(this.PATH_NAME);
				mRecorder.prepare();
			} catch (IllegalStateException e) {
				Log.d("recordingerror", e.getMessage());
			} catch (IOException e) {
				Log.d("recordingerror", e.getMessage());
			}
			mRecorder.start();
		} else {
			Log.d("recording", flat + "");
			if (mRecorder != null) {
				mRecorder.stop();
				mRecorder.release();
				mRecorder = null;
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.voice_demo, menu);
		return true;
	}

}
